﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataControl;
using System.Data;
using System.Configuration;

namespace DAL.Dao
{
    public class MessageDao
    {
        public string CreateMessageByRequestId(string strRequestId,string strType)
        {
            string strResult="";
            DataAccess da = new DataAccess();
            if (strType == "1")
            {
                string strSql = string.Format("select * from T_Request where RequestID='{0}'", strRequestId);
                DataTable dt = da.ExecuteDataTable(strSql);

                if (dt == null || dt.Rows.Count == 0)
                    return "";

                strResult = dt.Rows[0]["tauloginid"] + "客户于" + dt.Rows[0]["create_time"] + "创建的" + "询价单：" + dt.Rows[0]["title"] + "没有合适分供商进行报价，请登录" + ConfigurationManager.AppSettings["ManagerHost"] + "处理";
            }
            if (strType == "2")
            {
                string strTips = "";
                string strSql = string.Format(@"select t.*,case when t1.tauCountry IS null OR t1.tauCountry ='' then '中国' else t1.tauCountry end tauCountry,
                                            ISNULL(t2.CountryLan,'en') CountryLan
                                            from T_Request t
                                            inner join T_AppUser t1 on t.tauloginid=t1.tauLoginId 
                                            left join D_Country t2 on t1.tauCountry=t2.dcuDesc
                                            where RequestID='{0}'", strRequestId);
                DataTable dt = da.ExecuteDataTable(strSql);
                
                if (dt == null || dt.Rows.Count == 0)
                    return "";

                string strLan = "";
                strLan = dt.Rows[0]["CountryLan"].ToString();

                strTips = GetTips("7", strLan);




                strResult = dt.Rows[0]["title"] + strTips;
            }
            if (strType == "3")
            {
                string strSql = string.Format("select * from T_Request where RequestID='{0}'", strRequestId);
                DataTable dt = da.ExecuteDataTable(strSql);

                if (dt == null || dt.Rows.Count == 0)
                    return "";

                strResult = "您有新的询价单：" + dt.Rows[0]["title"] + "，请进行报价";
            }
            if (strType == "5")
            {
                //string strSql = string.Format("select * from T_AppUser where RequestID='{0}'", strRequestId);
                //DataTable dt = da.ExecuteDataTable(strSql);

                //if (dt == null || dt.Rows.Count > 0)
                //    return "";

                strResult = "客户" + strRequestId + "已过长时间未关怀，请进行关怀";
            }
            if (strType == "6")
            {

                strResult = "请重新确认您的报价";
            }
            if (strType == "7")
            {

                strResult = "请重新确认您的报价";
            }
            return strResult;
        }

        public string CreateMessageByDataTable(DataTable dtParm)
        {
            string strResult = "";
            DataAccess da = new DataAccess();
            foreach (DataRow dr in dtParm.Rows)
            {
                string strSql = @"select t2.title,t2.create_time,t.SID,
                                case t.args03 when '公制' then t8.Size_Code_C else t8.Size_Code_S end SIDDesc,
                                t5.[Desc] ResponseDesc,t1.tauloginid
                                from T_ResponseDetail t
                                inner join T_Response t1 on t.ResponseID=t1.ResponseID
                                inner join T_Request t2 on t1.RequestID=t2.RequestID
                                inner join T_RequestDetail t3 on t.ReqDetailID=t3.ReqDetailID
                                inner join T_Production_MultiLan t5 on t.ProductionID=t5.ProductionID and t5.LanId='zh'
                                inner join C_Size t8 on t.SID=t8.SID
                                where t.ReqDetailID='{0}'";
                strSql = string.Format(strSql, dr["ReqDetailID"].ToString());
                DataTable dt = da.ExecuteDataTable(strSql);
                if (dt == null || dt.Rows.Count == 0)
                    return "";

                strResult = "询价单" + dt.Rows[0]["title"] + "的" + dt.Rows[0]["tauloginid"] + "分供商报价存在问题，请登录" + ConfigurationManager.AppSettings["ManagerHost"] + "处理";
                
                //strResult = "询价单存在问题，请登录后台进行处理";

            }


            

            return strResult;
        }


        public string GetTips(string strType, string strLagId)
        {
            DataAccess da = new DataAccess();
            string strTips = "";

            string strSql = "select * from D_tips_multiLan where Tips_ID='{0}' and LanId='{1}'";
            strSql = string.Format(strSql, strType, strLagId);
            DataTable dt = da.ExecuteDataTable(strSql);

            if (dt == null && dt.Rows.Count == 0)
            {

                return strTips;
            }
            else
            {
                strTips = dt.Rows[0]["Desc"].ToString();
                return strTips;
            }
        }

        public string GetUserCountryLan(string strRequestId)
        {
            DataAccess da = new DataAccess();
            string strCountryLan = "";

            string strSql = string.Format(@"select t.*,case when t1.tauCountry IS null OR t1.tauCountry ='' then '中国' else t1.tauCountry end tauCountry,
                                            ISNULL(t2.CountryLan,'en') CountryLan
                                            from T_Request t
                                            inner join T_AppUser t1 on t.tauloginid=t1.tauLoginId 
                                            left join D_Country t2 on t1.tauCountry=t2.dcuDesc
                                            where RequestID='{0}'", strRequestId);
            DataTable dt = da.ExecuteDataTable(strSql);

            if (dt == null && dt.Rows.Count == 0)
            {

                return strCountryLan;
            }
            else
            {
                strCountryLan = dt.Rows[0]["CountryLan"].ToString();
                return strCountryLan;
            }
        }
    }
}
